package com.yywz.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class TestXML {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		DocumentBuilder builder = factory.newDocumentBuilder();
		
		File file = new File("test.xml");
		Document doc = builder.parse(file);
		
		Element root = doc.getDocumentElement();
		
		System.out.println(root.getNodeName());
		
		NodeList children = root.getChildNodes();
		
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i).getFirstChild();
			if(child instanceof Element){
				Element childElement = (Element) child;
				System.out.println(childElement.getNodeName());
				Text textNode = (Text) childElement.getFirstChild();
				System.out.println(textNode.getData());
			}
		}
		
	}

}
